package com.crane.oss.web.controller.sys;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.Response;
import com.crane.common.core.dto.SessParty;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.enums.ConstantEnums;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.sys.bean.request.role.AddRoleRequest;
import com.crane.oss.sys.bean.request.role.QueryRoleRequest;
import com.crane.oss.sys.bean.request.role.UpdateRoleRequest;
import com.crane.oss.sys.bean.resp.RoleBean;
import com.crane.oss.sys.bean.resp.UserBean;
import com.crane.oss.enums.RoleTypeEnum;
import com.crane.oss.sys.service.RoleService;
import com.crane.oss.sys.service.UserRoleService;
import com.crane.oss.web.BaseBusinessController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2018年12月28日 19时42分58秒
 */

@Api(value = "RoleController", tags = {"系统管理-角色管理"})
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseBusinessController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation(value = "获取列表信息", notes = "获取列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryRoleRequest")
    @RequiresPermissions("sys:role:list")
    @PostMapping("/list")
    public MultiResponse list(@RequestBody QueryRoleRequest request) {
        // 查询列表数据
        request.setCreateUserId(getUserId());
        SessParty sessParty = getParty();
        if (null != sessParty) {
            request.setCreatePartyId(sessParty.getPartyId());
        }
        IPage<RoleBean> page = roleService.queryList(request);
        return MultiResponse.of(page);
    }

    @ApiOperation(value = "获取详细信息", notes = "获取详细信息")
    @RequiresPermissions("sys:role:info")
    @PostMapping("/info")
    public SingleResponse info(@RequestBody String data) {
        String sid = getParamter(data, "id");
        RoleBean roleBean = roleService.queryRoleAndMenuList(Long.parseLong(sid), getUser());
        return SingleResponse.of(roleBean);
    }

    /**
     * 查询自己创建的角色
     */
    @ApiOperation(value = "获取用户创建的角色信息", notes = "获取用户创建的角色信息")
    @GetMapping(value = "/getRoleByUser")
    @RequiresAuthentication
    public MultiResponse getRoleByUser() {
        List<RoleBean> list = roleService.queryRoleListByCreateUser(getUser(), getParty());
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "获取系统角色信息", notes = "获取用户创建的角色信息")
    @GetMapping(value = "/getSysRole")
    @RequiresAuthentication
    public MultiResponse getSysRole() {
        QueryRoleRequest queryRoleRequest = new QueryRoleRequest();
        queryRoleRequest.setStatus(ConstantEnums.STATUS.VALID.getValue());
        queryRoleRequest.setType(RoleTypeEnum.SYSTEM_ADMIN.getType());
        IPage<RoleBean> page = roleService.queryList(queryRoleRequest);
        return MultiResponse.ofWithoutTotal(page.getRecords());
    }

    /**
     * 查询当前用户所属角色列表,是超级管理员返回空
     */
    @ApiOperation(value = "获取用户所属的角色信息", notes = "获取用户所属的角色信息")
    @GetMapping(value = "/getUserRole")
    @RequiresAuthentication
    public MultiResponse getUserRole() {
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(getUser(), userBean);
        List<RoleBean> list = roleService.getUserRole(userBean);
        return MultiResponse.ofWithoutTotal(list);
    }

    @ApiOperation(value = "查询角色是否绑定用户", notes = "查询角色是否绑定用户")
    @PostMapping("/roleUser")
    @RequiresAuthentication
    public SingleResponse getRoleUser(@RequestBody String data) {
        String sRoleId = getParamter(data, "roleId");
        List<Long> list = userRoleService.queryUserIdList(Long.parseLong(sRoleId));
        if (CollectionUtils.isEmpty(list)) {
            return SingleResponse.of(false);
        } else {
            return SingleResponse.of(true);
        }
    }

    @ApiOperation(value = "新增角色对应菜单信息", notes = "新增角色对应菜单信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AddRoleRequest")
    @RequiresPermissions("sys:role:save") // @SysLog("保存")
    @PostMapping("/save")
    public Response save(@RequestBody @Valid AddRoleRequest request) {
        roleService.save(request, getUser());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "修改角色对应菜单信息", notes = "修改角色对应菜单信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "UpdateRoleRequest")
    @RequiresPermissions("sys:role:update") // @SysLog("修改")
    @PostMapping("/update")
    public Response update(@RequestBody @Valid UpdateRoleRequest request) {
        roleService.update(request, getUser());
        return Response.buildSuccess();
    }

    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    @RequiresPermissions("sys:role:delete") // @SysLog(value="删除",self=true)
    @PostMapping("/delete")
    public Response delete(@RequestBody String data) {
        String sid = getParamter(data, "id");
        roleService.delete(Long.parseLong(sid), getUser());
        return Response.buildSuccess();
    }

}
